package com.yzkj.cpjxc.business.service.impl;

import com.yzkj.cpjxc.business.framework.common.BusinessMap;
import com.yzkj.cpjxc.business.framework.common.Constants;
import com.yzkj.cpjxc.business.framework.common.Pager;
import com.yzkj.cpjxc.business.framework.utils.IDUtils;
import com.yzkj.cpjxc.business.domain.PingZhengChild;
import com.yzkj.cpjxc.business.domain.PingZhengChildQueryVo;
import com.yzkj.cpjxc.business.domain.PingZhengChildShowVo;
import com.yzkj.cpjxc.business.service.PingZhengChildService;
import org.apache.ibatis.session.RowBounds;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 凭证管理关联子表 Service 实现
 */
@Service
public class PingZhengChildServiceImpl implements PingZhengChildService {

	/**
	 * MyBatis 命名空间
	 */
	private final static String MAPPER_NAMESPACE = "com.chkj.xezz.pingZhengChild.mappers.PingZhengChildMapper";

	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;
	
	@Override
	public BusinessMap create(PingZhengChild pingZhengChild) throws Exception {
		BusinessMap bm = new BusinessMap();
		if (this.isExists(pingZhengChild)) {
			bm.put(Constants.WARNING_MSG, "该凭证管理关联子表名称已经存在");
			return bm;
		}
		pingZhengChild.setUuid(IDUtils.genUUID());
		sqlSessionTemplate.insert(MAPPER_NAMESPACE + ".add", pingZhengChild);
		return bm;
	}

	@Override
	public boolean isExists(PingZhengChild pingZhengChild) throws Exception {
		return (Integer)sqlSessionTemplate.selectOne(MAPPER_NAMESPACE + ".isExists", pingZhengChild) > 0;
	}
	
	@Override
	public PingZhengChild getById(String uuid) throws Exception {
		return sqlSessionTemplate.selectOne(MAPPER_NAMESPACE + ".getById", uuid);
	}
	
	@Override
	public BusinessMap edit(PingZhengChild pingZhengChild) throws Exception {
		BusinessMap bm = new BusinessMap();
		if (this.isExists(pingZhengChild)) {
			bm.put(Constants.WARNING_MSG, "该凭证管理关联子表名称已经存在");
			return bm;
		}
		sqlSessionTemplate.update(MAPPER_NAMESPACE + ".edit", pingZhengChild);
		return bm;
	}

	@Override
	public boolean delete(String uuid) throws Exception {
		sqlSessionTemplate.delete(MAPPER_NAMESPACE + ".deleteById", uuid);
		return true;
	}

	@Override
	public Pager<PingZhengChild> search(Pager<PingZhengChild> pager, PingZhengChildQueryVo queryVo) throws Exception {
		List<PingZhengChild> dataList = sqlSessionTemplate.selectList(MAPPER_NAMESPACE + ".page", queryVo, new RowBounds((pager.getCurrentPage() - 1) * pager.getLimit(), pager.getLimit()));
		pager.setDataList(dataList);
		
		// 如果需要转换成ShowVo展示，那么就在这里转换
		
		Long total = sqlSessionTemplate.selectOne(MAPPER_NAMESPACE + ".getTotal", queryVo);
		pager.setTotal(total);
		return pager;
	}

	@Override
	public List<PingZhengChildShowVo> getBypzid(String pzId) throws Exception {
		List<PingZhengChildShowVo> dataList = sqlSessionTemplate.selectList(MAPPER_NAMESPACE + ".getBypzid", pzId);
		return dataList;
	}
	
	@Override
	public boolean deleteChlids(String pzId) throws Exception {
		sqlSessionTemplate.delete(MAPPER_NAMESPACE + ".deleteChlids", pzId);
		return true;
	}

	@Override
	public PingZhengChild getMaxSequenceNumberByDate(PingZhengChild pingZhengChild) throws Exception {
		return sqlSessionTemplate.selectOne(MAPPER_NAMESPACE + ".getMaxSequenceNumberByDate", pingZhengChild);
	}

	@Override
	public void updateMaxDataToUp(PingZhengChild pingZhengChild, double increment) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pingZhengChild", pingZhengChild);
		map.put("increment", increment);
		sqlSessionTemplate.update(MAPPER_NAMESPACE + ".updateMaxDataToUp", map);
	}

	@Override
	public void updateMaxDataToDown(PingZhengChildShowVo vo, double increment) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pingZhengChild", vo);
		map.put("increment", increment);
		sqlSessionTemplate.update(MAPPER_NAMESPACE + ".updateMaxDataToDown", map);
	}

}
